package com.tqjc.business.interactivewithscan.uils;

import com.tqjc.business.interactivewithscan.consts.DeviceEnum;
import com.tqjc.business.interactivewithscan.consts.ExceptionEnum;
import com.tqjc.business.interactivewithscan.entity.SameSpanTianche;
import com.tqjc.business.interactivewithscan.exception.CustomException;
import com.tqjc.business.interactivewithscan.service.UfcDeviceInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TiancheUtils {
    private static Logger logger = LoggerFactory.getLogger(TiancheUtils.class);

    /**
     * 查询天车操作模式
     *
     * @param deviceCode
     * @return
     */
//    public static boolean queryTiancheMode(String deviceCode) {
//        SlagSpanTiancheDevice thisTianche = DeviceThreadPool.devices.getObject(deviceCode, SlagSpanTiancheDevice.class);
//        if (thisTianche.getSocketClient() == null || thisTianche.getSocketClient().getSocket() == null) {
//            throw new CustomException(ExceptionEnum.THREAD_NO_START.getCode(), SlagSpanTiancheDevice.class.getTypeName() + "线程未启动");
//        }
//        DeviceCommandData<Boolean> data = new DeviceCommandData(thisTianche.getDevice(), thisTianche.getSocketClient(), ActionEnum.Query_Tianche_Mode, Boolean.class);
//        data.setLog(false);
//        Boolean a = SocketCommandUtils.sendCommand(data);
//        if (a == null) {
//            return false;
//        }
//        return a;
//    }
//
//    /**
//     * 大车移动到指定位置
//     *
//     * @param target 目标X轴信息
//     * @return
//     */
//    public static boolean moveTianche(TiancheDevice tiancheDevice, Integer target, ReentrantLock lock, String actionNo) {
//        DeviceSocketClient socketClient = tiancheDevice.getSocketClient();
//        tiancheDevice.setActionNo(actionNo == null ? obtainAction() : actionNo);
//        Device device = socketClient.getDevice();
//        /* 设置大车目标坐标 */
//        DeviceCommandData<ScadaCommandEntity> commandData = new DeviceCommandData(device, socketClient, ActionEnum.Crane_Target_Coordinates, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置大车目标坐标", target);
//        SocketCommandUtils.sendCommand(commandData);
//        /* 设置作业类型 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.Job_Type, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置作业类型", 3);
//        SocketCommandUtils.sendCommand(commandData);
//        /* 设置任务运行状态 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.Task_Running, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置任务运行状态", 1);
//        SocketCommandUtils.sendCommand(commandData);
//        if (lock != null) {
//            lock.unlock();
//        }
//        sleep(500);
//        /* 查询避让任务完成状态 */
//        DeviceCommandData<Boolean> avoidData = new DeviceCommandData(device, socketClient, ActionEnum.Avoid_Task, Boolean.class);
//        while (true) {
//            sleep(500);
//            Boolean r = SocketCommandUtils.sendCommand(avoidData);
//            if (!EmptyUtil.isEmpty(r) && r) {
//                break;
//            }
//        }
//        sleep(500);
//        /* 设置任务运行状态 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.Task_Running, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置任务运行状态", 0);
//        SocketCommandUtils.sendCommand(commandData);
//        sleep(500);
//        return true;
//    }
//
//    /**
//     * 大车实时位置
//     *
//     * @return
//     */
//    public static Integer realTimeLocation(DeviceSocketClient socketClient, Device device) {
//        return realTimeLocation(socketClient, device, true);
//    }
//
//    public static Integer realTimeLocation(DeviceSocketClient socketClient, Device device, boolean isLog) {
//        try {
//            DeviceCommandData<Integer> data = new DeviceCommandData(device, socketClient, ActionEnum.Crane_REALTIME_LOCATION, Integer.class);
//            data.setLog(isLog);
//            Integer location = SocketCommandUtils.sendCommand(data);
//            if (location == null || location.equals(0)) {
//                System.err.println("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【查询大车实时位置】结果有误：" + location);
//                sleep(500);
//                return realTimeLocation(socketClient, device, isLog);
//            }
//            return location;
//        } catch (Exception e) {
//            System.err.println("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【查询大车实时位置】结果有误！");
//            e.printStackTrace();
//            sleep(500);
//            return realTimeLocation(socketClient, device, isLog);
//        }
//    }
//
//    /**
//     * 执行天车急停
//     *
//     * @param socketClient
//     * @param device
//     * @param stop         true：急停，false：复位
//     * @return
//     */
//    public static boolean emergencyStop(DeviceSocketClient socketClient, Device device, boolean stop) {
//        try {
//            DeviceCommandData<Boolean> data = new DeviceCommandData(device, socketClient, ActionEnum.Emergency_Stop, Boolean.class);
//            data.getDynamicValues().put("天车急停", stop);
//            SocketCommandUtils.sendCommand(data);
//            return true;
//        } catch (Exception e) {
//            System.err.println("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【天车急停】结果有误！");
//            return false;
//        }
//    }
//
//    /**
//     * 修改天车操作模式
//     *
//     * @param socketClient
//     * @param device
//     * @param mode
//     * @return
//     */
//    public static boolean updateTiancheMode(DeviceSocketClient socketClient, Device device, boolean mode) {
//        try {
//            DeviceCommandData<Boolean> data = new DeviceCommandData(device, socketClient, ActionEnum.Update_Tianche_Mode, Boolean.class);
//            data.getDynamicValues().put("操作模式", mode);
//            SocketCommandUtils.sendCommand(data);
//            return true;
//        } catch (Exception e) {
//            System.err.println("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【天车急停】结果有误！");
//            return false;
//        }
//    }
//
//    /**
//     * 设置对方实时位置
//     *
//     * @param socketClient
//     * @param device
//     * @param location
//     * @return
//     */
//    public static boolean setLocationToOther(DeviceSocketClient socketClient, Device device, int location, boolean log) {
//        try {
//            DeviceCommandData<Boolean> data = new DeviceCommandData(device, socketClient, ActionEnum.Set_ESP_To_Other, Boolean.class);
//            data.getDynamicValues().put("当前ESP", location);
//            data.setLog(log);
//            SocketCommandUtils.sendCommand(data);
//            return true;
//        } catch (Exception e) {
//            System.err.println("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【设置对方实时位置】结果有误！");
//            return false;
//        }
//    }
//
//    /**
//     * 设置当前ESP到同跨另一天车
//     *
//     * @param socketClient
//     * @param device
//     * @return
//     */
//    public static boolean commonSetting(DeviceSocketClient socketClient, Device device, ActionEnum actionEnum, JSONObject dynamicValues, boolean log) {
//        try {
//            DeviceCommandData<Boolean> data = new DeviceCommandData(device, socketClient, actionEnum, Boolean.class);
//            data.getDynamicValues().putAll(dynamicValues);
//            data.setLog(log);
//            SocketCommandUtils.sendCommand(data);
//            return true;
//        } catch (Exception e) {
//            GiveAnAlarm.implement("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【" + actionEnum.getActionName() + "】结果有误！" + dynamicValues.toJSONString());
//            return false;
//        }
//    }
//
//    /**
//     * 设置当前ESP到同跨另一天车
//     *
//     * @param socketClient
//     * @param device
//     * @param esp
//     * @return
//     */
//    public static boolean setESPToOther(DeviceSocketClient socketClient, Device device, Double esp, boolean log) {
//        try {
//            DeviceCommandData<Boolean> data = new DeviceCommandData(device, socketClient, ActionEnum.Set_ESP_To_Other, Boolean.class);
//            data.getDynamicValues().put("当前ESP", esp);
//            data.setLog(log);
//            SocketCommandUtils.sendCommand(data);
//            return true;
//        } catch (Exception e) {
//            GiveAnAlarm.implement("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【设置当前ESP到同跨另一天车】结果有误！");
//            return false;
//        }
//    }
//
//    /**
//     * 查询当前ESP
//     *
//     * @param socketClient
//     * @param device
//     * @return
//     */
//    public static Double queryCurrentESP(DeviceSocketClient socketClient, Device device, boolean log) {
//        try {
//            DeviceCommandData<Double> data = new DeviceCommandData(device, socketClient, ActionEnum.Query_Current_ESP, Double.class);
//            data.setLog(log);
//            Double esp = SocketCommandUtils.sendCommand(data);
//            return esp;
//        } catch (Exception e) {
//            GiveAnAlarm.implement("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【查询当前ESP】结果有误！");
//            return null;
//        }
//    }
//
//    /**
//     * 查询大车抱闸状态
//     *
//     * @param socketClient
//     * @param device
//     * @return
//     */
//    public static Boolean queryCraneHoldingBrake(DeviceSocketClient socketClient, Device device, boolean log) {
//        try {
//            DeviceCommandData<Boolean> data = new DeviceCommandData(device, socketClient, ActionEnum.Query_Crane_Holding_Brake, Boolean.class);
//            data.setLog(log);
//            Boolean brake = SocketCommandUtils.sendCommand(data);
//            if (brake == null) {
//                System.err.println("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【查询大车抱闸状态】没有查到！");
//                return queryCraneHoldingBrake(socketClient, device, log);
//            }
//            return brake;
//        } catch (Exception e) {
//            System.err.println("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【查询大车抱闸状态】结果有误！");
//            e.printStackTrace();
//            sleep(500);
//            return queryCraneHoldingBrake(socketClient, device, log);
//        }
//    }
//
//    /**
//     * 查询大车起升高度
//     *
//     * @param socketClient
//     * @param device
//     * @return
//     */
//    public static Integer queryLiftHeight(DeviceSocketClient socketClient, Device device) {
//        try {
//            DeviceCommandData<Integer> data = new DeviceCommandData(device, socketClient, ActionEnum.Query_Lift_Height, Integer.class);
//            Integer height = SocketCommandUtils.sendCommand(data);
//            if (height == null) {
//                System.err.println("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【查询大车起升高度】没有查到！");
//                return queryLiftHeight(socketClient, device);
//            }
//            return height;
//        } catch (Exception e) {
//            System.err.println("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【查询大车起升高度】结果有误！");
//            e.printStackTrace();
//            sleep(500);
//            return queryLiftHeight(socketClient, device);
//        }
//    }
//
//    /**
//     * 查询任务运行状态
//     *
//     * @return
//     */
//    public static Boolean queryTaskRunning(DeviceSocketClient socketClient, Device device) {
//        try {
//            DeviceCommandData<Boolean> data = new DeviceCommandData(device, socketClient, ActionEnum.Query_Task_Running, Boolean.class);
//            Boolean taskRunning = SocketCommandUtils.sendCommand(data);
//            if (taskRunning == null) {
//                System.err.println("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【查询任务运行状态】没有查到！");
//                return queryTaskRunning(socketClient, device);
//            }
//            return taskRunning;
//        } catch (Exception e) {
//            System.err.println("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【查询任务运行状态】结果有误！");
//            e.printStackTrace();
//            sleep(500);
//            return queryTaskRunning(socketClient, device);
//        }
//    }
//
//    /**
//     * 大车目标位置
//     *
//     * @return
//     */
////    public static Integer targetLocation(DeviceSocketClient socketClient, Device device) {
////        return targetLocation(socketClient, device, true);
////    }
//    public static Integer targetLocation(TiancheDevice tiancheDevice, Device device, boolean isErr) {
//        if (tiancheDevice.getTarget() != 0) {
//            System.err.println("【" + device.getDeviceName() + "】指定设备【" + tiancheDevice.getSocketClient().getDevice().getDeviceName() + "】执行【查询大车目标位置】直接从系统中获取的结果：" + tiancheDevice.getTarget());
//            return tiancheDevice.getTarget();
//        }
//        DeviceCommandData<Integer> data = new DeviceCommandData(device, tiancheDevice.getSocketClient(), ActionEnum.Crane_Target_LOCATION, Integer.class);
//        Integer target = SocketCommandUtils.sendCommand(data);
//        if (target == null || target.equals(0) || target < 0) {
//            System.err.println("【" + device.getDeviceName() + "】指定设备【" + tiancheDevice.getSocketClient().getDevice().getDeviceName() + "】执行【查询大车目标位置】结果有误：" + target);
//            if (isErr) {
//                throw new CustomException(ExceptionEnum.TIANCHE_COMMAND_ERROR);
//            }
//            sleep(500);
//            return targetLocation(tiancheDevice, device, false);
//        }
//        return target;
//    }
//
//    /**
//     * 天车抓料任务
//     *
//     * @param thisDevice
//     * @param target
//     * @param type       1：去槽车作业 2：去库区作业 3：去货车作业
//     * @return
//     */
//    public static boolean moveGraspingTask(TiancheDevice thisDevice, Coord target, Integer type) {
//        DeviceSocketClient socketClient = thisDevice.getSocketClient();
//        System.err.println("【" + socketClient.getDevice().getDeviceName() + "】吸料坐标【" + target + "】");
//        Device device = socketClient.getDevice();
//        /* 设置大车目标坐标 */
//        thisDevice.setTarget(target.getX());
//        thisDevice.setActionNo(obtainAction());
//        DeviceCommandData<ScadaCommandEntity> commandData = new DeviceCommandData(device, socketClient, ActionEnum.Crane_Target_Coordinates, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置大车目标坐标", target.getX());
//        SocketCommandUtils.sendCommand(commandData);
//        /* 设置小车目标坐标 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.Trolley_Target_Coordinates, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置小车目标坐标", target.getY());
//        SocketCommandUtils.sendCommand(commandData);
//        /* 设置起升目标坐标 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.Lifting_Target_Coordinates, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置起升目标坐标", target.getZ() - 1000);
//        SocketCommandUtils.sendCommand(commandData);
//        /* 避让校验 */
//        try {
//            AvoidanceUtils.avoidance(thisDevice);
//            commandData = new DeviceCommandData(device, socketClient, ActionEnum.Crane_Target_Coordinates, ScadaCommandEntity.class);
//        } catch (CustomException e) {
//            System.err.println(e.getMsg());
//            if (e.getCode() == ExceptionEnum.TIANCHE_COMMAND_ERROR.getCode()) {
//                AvoidanceUtils.avoidance(thisDevice);
//
//            } else {
//                throw e;
//            }
//        }
//        /* 设置大车目标坐标 */
//        commandData.getDynamicValues().put("设置大车目标坐标", target.getX());
//        SocketCommandUtils.sendCommand(commandData);
//        /* 设置PLC任务 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.PLC_Task, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置PLC任务", type);
//        SocketCommandUtils.sendCommand(commandData);
//        /* 设置作业类型 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.Job_Type, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置作业类型", 1);
//        SocketCommandUtils.sendCommand(commandData);
//        sleep(1000);        /* 设置任务运行状态 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.Task_Running, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置任务运行状态", 1);
//        SocketCommandUtils.sendCommand(commandData);
//        /* 查询吸料任务步骤 */
//        int index = 0;
//        while (true) {
//            sleep(500);
//            Integer steps = queryGrabSteps(socketClient, device);
//            if (!steps.equals(index)) {
//                if (steps == 1) {
//                    System.err.println("【" + device.getDeviceName() + "】正在执行吸料步骤【吸磁前将起升升到安全高度】");
//                }
//                if (steps == 2) {
//                    System.err.println("【" + device.getDeviceName() + "】正在执行吸料步骤【移动天车到目标位置】");
//                }
//                if (steps == 3) {
//                    System.err.println("【" + device.getDeviceName() + "】正在执行吸料步骤【移动起升到目标高度】");
//                }
//                if (steps == 4) {
//                    System.err.println("【" + device.getDeviceName() + "】正在执行吸料步骤【探料吸料】");
//                    break;
//                }
//            }
//            index = steps;
//        }
//        /* 查询吸料任务完成状态 */
//        while (true) {
//            sleep(500);
//            DeviceCommandData<Boolean> suctionData = new DeviceCommandData(device, socketClient, ActionEnum.Suction_Task, Boolean.class);
//            Boolean r = SocketCommandUtils.sendCommand(suctionData);
//            if (!EmptyUtil.isEmpty(r) && r) {
//                break;
//            }
//        }
//        thisDevice.setTarget(0);
//        thisDevice.setActionNo("");
//        sleep(500);
//        /* 设置任务运行状态 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.Task_Running, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置任务运行状态", 0);
//        SocketCommandUtils.sendCommand(commandData);
//        System.err.println("【" + device.getDeviceName() + "】吸料结束");
//        return true;
//    }
//
//    /**
//     * 天车卸料任务
//     *
//     * @param thisDevice
//     * @param tiancheTask
//     * @param type        1：去槽车作业 2：去库区作业 3：去货车作业
//     * @return
//     */
//    public static boolean moveDischargeTask(TiancheDevice thisDevice, UfcTiancheTask tiancheTask, Integer type) {
//        DeviceSocketClient socketClient = thisDevice.getSocketClient();
//        Coord target = tiancheTask.getStayDischargeCoord();
//        Device device = socketClient.getDevice();
//        System.err.println("【" + device.getDeviceName() + "】放料坐标【" + target + "】");
//        /* 设置大车目标坐标 */
//        thisDevice.setTarget(target.getX());
//        thisDevice.setActionNo(obtainAction());
//        DeviceCommandData<ScadaCommandEntity> commandData = new DeviceCommandData(device, socketClient, ActionEnum.Crane_Target_Coordinates, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置大车目标坐标", target.getX());
//        SocketCommandUtils.sendCommand(commandData);
//
//        SlagSpanTiancheDevice thisTianche = DeviceThreadPool.devices.getObject(device.getDeviceCode(), SlagSpanTiancheDevice.class);
//        TankerDevice tankerDevice = thisTianche.queryCurrentTanker();
//        /* 设置小车目标坐标 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.Trolley_Target_Coordinates, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置小车目标坐标", target.getY());
//        SocketCommandUtils.sendCommand(commandData);
//
//        /* 设置起升目标坐标 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.Lifting_Target_Coordinates, ScadaCommandEntity.class);
//        if (target.getZ() > 12000) {
//            target.setZ(12000);
//        }
//        commandData.getDynamicValues().put("设置起升目标坐标", target.getZ() - 1500);
//        SocketCommandUtils.sendCommand(commandData);
//
//        /* 避让校验 */
//        try {
//            AvoidanceUtils.avoidance(thisDevice);
//        } catch (CustomException e) {
//            System.err.println(e.getMsg());
//            if (e.getCode() == ExceptionEnum.TIANCHE_COMMAND_ERROR.getCode()) {
//                AvoidanceUtils.avoidance(thisDevice);
//            } else {
//                throw e;
//            }
//        }
//        /* 设置大车目标坐标 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.Crane_Target_Coordinates, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置大车目标坐标", target.getX());
//        SocketCommandUtils.sendCommand(commandData);
//
//        /* 设置PLC任务 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.PLC_Task, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置PLC任务", type);
//        SocketCommandUtils.sendCommand(commandData);
//
//        /* 设置作业类型 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.Job_Type, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置作业类型", 2);
//        SocketCommandUtils.sendCommand(commandData);
//        sleep(1000);
//
//        /* 设置任务运行状态 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.Task_Running, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置任务运行状态", 1);
//        SocketCommandUtils.sendCommand(commandData);
//
//        /* 查询放料任务步骤 */
//        int index = 0;
//        BigDecimal weight = new BigDecimal(0);
//        while (true) {
//            sleep(500);
//            Integer steps = queryDischargeSteps(socketClient, device);
//            if (!steps.equals(index)) {
//                if (steps == 1) {
//                    System.err.println("【" + device.getDeviceName() + "】正在执行放料步骤【移动大车小车到目标位置正上方】");
//                }
//                if (steps == 2) {
//                    if (type.equals(1)) {
//                        weight = tankerDevice.queryCurrentWeight(null);
//                    }
//                    System.err.println("【" + device.getDeviceName() + "】正在执行放料步骤【移动起升到目标位置】");
//                }
//                if (steps == 3) {
//                    System.err.println("【" + device.getDeviceName() + "】正在执行放料步骤【放磁】");
//                }
//                if (steps == 4) {
//                    BigDecimal temp = tankerDevice.queryCurrentWeight(null);
//                    System.err.println("【" + device.getDeviceName() + "】槽车卸料前称重：" + weight);
//                    System.err.println("【" + device.getDeviceName() + "】槽车卸料后称重：" + temp);
//                    weight = temp.subtract(weight);
//                    System.err.println("【" + device.getDeviceName() + "】槽车增加重量：" + weight);
//                    tankerDevice.removeTask(tiancheTask, weight);
//                    System.err.println("【" + device.getDeviceName() + "】正在执行放料步骤【起升升到安全高度】");
//                    break;
//                }
//            }
//            index = steps;
//        }
//        /* 查询放料任务完成状态 */
//        while (true) {
//            sleep(500);
//            DeviceCommandData<Boolean> dischargeData = new DeviceCommandData(device, socketClient, ActionEnum.Discharge_Task, Boolean.class);
//            Boolean r = SocketCommandUtils.sendCommand(dischargeData);
//            if (!EmptyUtil.isEmpty(r) && r) {
//                break;
//            }
//        }
//        thisDevice.setTarget(0);
//        thisDevice.setActionNo("");
//        sleep(500);
//
//        /* 设置任务运行状态 */
//        commandData = new DeviceCommandData(device, socketClient, ActionEnum.Task_Running, ScadaCommandEntity.class);
//        commandData.getDynamicValues().put("设置任务运行状态", 0);
//        SocketCommandUtils.sendCommand(commandData);
//        System.err.println("【" + device.getDeviceName() + "】放料结束");
//        return true;
//    }
//
//    public static void sleep(int times) {
//        try {
//            Thread.sleep(times);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 获取同跨另一台天车的设备编码
     *
     * @param deviceCode
     * @return
     */
    public static String otherTiancheCode(String deviceCode) {
        /*炉渣跨天车*/
        if (DeviceEnum.Tianche_Slag_1.getDeviceCode().equals(deviceCode)) {
            return DeviceEnum.Tianche_Slag_2.getDeviceCode();
        }
        if (DeviceEnum.Tianche_Slag_2.getDeviceCode().equals(deviceCode)) {
            return DeviceEnum.Tianche_Slag_1.getDeviceCode();
        }
        /*废钢跨天车*/
        if (DeviceEnum.Tianche_Steel_1.getDeviceCode().equals(deviceCode)) {
            return DeviceEnum.Tianche_Steel_2.getDeviceCode();
        }
        if (DeviceEnum.Tianche_Steel_2.getDeviceCode().equals(deviceCode)) {
            return DeviceEnum.Tianche_Steel_1.getDeviceCode();
        }
        return "";
    }

    public static SameSpanTianche settingTiancheInfo(UfcDeviceInfoService deviceInfoService, Integer type, boolean t) {
        String tiancheCode = "";
        if (type.equals(1)) {
            if (t) {
                tiancheCode = DeviceEnum.Tianche_Steel_1.getDeviceCode();
            } else {
                tiancheCode = DeviceEnum.Tianche_Steel_2.getDeviceCode();
            }
        }
        if (type.equals(2)) {
            if (t) {
                tiancheCode = DeviceEnum.Tianche_Slag_1.getDeviceCode();
            } else {
                tiancheCode = DeviceEnum.Tianche_Slag_2.getDeviceCode();
            }
        }
        if (EmptyUtil.isEmpty(tiancheCode)) {
            throw new CustomException(ExceptionEnum.SCANNER_DEVICE_ERROR);
        }
        SameSpanTianche tiancheInfo = new SameSpanTianche(deviceInfoService.queryByCode(tiancheCode), deviceInfoService.queryByCode(TiancheUtils.otherTiancheCode(tiancheCode)));
        return tiancheInfo;
    }

//    public static Integer queryGrabSteps(DeviceSocketClient socketClient, Device device) {
//        DeviceCommandData<Integer> data = new DeviceCommandData(device, socketClient, ActionEnum.Query_Grab_Steps, Integer.class);
//        data.setLog(true);
//        Integer steps = SocketCommandUtils.sendCommand(data);
//        if (steps == null || steps.equals(0) || steps < 0) {
//            System.err.println("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【查询天车吸料步骤】结果有误：" + steps);
//            return -1;
//        }
//        return steps;
//    }
//
//    public static Integer queryDischargeSteps(DeviceSocketClient socketClient, Device device) {
//        DeviceCommandData<Integer> data = new DeviceCommandData(device, socketClient, ActionEnum.Query_Discharge_Steps, Integer.class);
//        data.setLog(true);
//        Integer steps = SocketCommandUtils.sendCommand(data);
//        if (steps == null || steps.equals(0) || steps < 0) {
//            System.err.println("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【查询天车放料步骤】结果有误：" + steps);
//            return -1;
//        }
//        return steps;
//    }
//
//    /**
//     * 查询作业类型
//     *
//     * @return
//     */
//    public static Integer queryJobType(DeviceSocketClient socketClient, Device device) {
//        try {
//            DeviceCommandData<Integer> data = new DeviceCommandData(device, socketClient, ActionEnum.Query_Job_Type, Integer.class);
//            Integer jobType = SocketCommandUtils.sendCommand(data);
//            if (jobType == null) {
//                System.err.println("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【查询作业类型】没有查到！");
//                return queryJobType(socketClient, device);
//            }
//            return jobType;
//        } catch (Exception e) {
//            System.err.println("【" + device.getDeviceName() + "】指定设备【" + socketClient.getDevice().getDeviceName() + "】执行【查询作业类型】结果有误！");
//            e.printStackTrace();
//            sleep(500);
//            return queryJobType(socketClient, device);
//        }
//    }
//
//    public static String obtainAction() {
//        Long l = System.currentTimeMillis() % 10000000000L;
//        return l.toString();
//    }
}